<template>
  <Panel>
    <template v-slot:header>
      <div>管理员信息</div>
    </template>
    <template v-slot:con>
      <ul>
        <li>
          <strong>管理员ID:</strong>
          <span>{{ userlist.id }}</span>
        </li>
        <li>
          <strong>账号:</strong>
          <span>{{ userlist.account }}</span>
        </li>
        <li>
          <strong>用户组：</strong>
          <span>{{ userlist.userGroup }}</span>
        </li>
        <li>
          <strong>创建时间：</strong>
          <span>{{ userlist.ctime | getTime }}</span>
        </li>
        <li>
          <strong>头像：</strong>
          <el-upload
            class="avatar-uploader"
            action="http://127.0.0.1:5000/users/avatar_upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </li>
        <li>
          <el-button type="primary" @click="editavatar">保存</el-button>
        </li>
      </ul>
    </template>
  </Panel>
</template>


<script>
import moment from "moment";
import { editAvatar } from "../../api/user.js";
import local from "../../utils/local.js";
export default {
  created() {
    this.userlist = JSON.parse(local.get("users"));
  },
  data() {
    return {
      userlist: [],
      imageUrl: "",
      imgUrl: "",
    };
  },
  filters: {
    getTime(val) {
      return moment(val).format("YYYY-MM-DD HH:mm:ss");
    },
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.imgUrl = res.imgUrl;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    async editavatar() {
      console.log(11);
      const data = await editAvatar({
        imgUrl: this.imgUrl,
      });
      if (data.code === 0) {
        this.$message({
          message: data.msg,
          type: "success",
        });
        location.reload();
      }
    },
  },
};
</script>

<style lang="less" scoped>
li {
  padding: 20px;
  strong {
    margin-right: 10px;
  }
}
//头像
/deep/.avatar-uploader .el-upload {
  margin-top: 10px;
  margin-left: 60px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 128px;
  height: 128px;
  line-height: 128px;
  text-align: center;
}
.avatar {
  width: 128px;
  height: 128px;
  display: block;
}
</style>